package com.nation.serviceImpl;

import com.nation.dao.NationInfoDao;
import com.nation.domain.TCountry;
import com.nation.domain.TNationInfo;
import com.nation.service.NationInfoService;
import com.nation.utils.ReadAndStoreFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by dqd on 2017/11/2.
 */
@Service
public class NaitonInfoServiceImpl implements NationInfoService {
    @Autowired
    NationInfoDao NationInfoDao;
    @Override
    public List<TNationInfo> findAllNationInfo() {
        return NationInfoDao.findAllNationInfo();
    }

    @Override
    public TNationInfo findByCountryCode(String country_code) {
        return NationInfoDao.findByCountryCode(country_code);
    }

    @Override
    public Boolean updateNationInfo(TNationInfo nationInfo) {
        return NationInfoDao.updateNationInfo(nationInfo);
    }

    @Override
    public List<TNationInfo> findByCondition(Map<String, Object> map) {
        return NationInfoDao.findByCondition(map);
    }

    @Override
    public void uploadNationFile(List<String> picture_paths,String input_code,int selectFileKind){
//        List nation_es=NationInfoDao.findAllNationInfo();
        for (String picture_path : picture_paths){
            String nation_z= ReadAndStoreFile.getImgName(picture_path);
            System.out.println("naiton_z=="+nation_z);
            Date currentTime=new Date();
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            String input_time=simpleDateFormat.format(currentTime);
            System.out.println("picture_path==="+picture_path);
            TNationInfo nationInfo=new TNationInfo();
            nationInfo.setRecoder_id(input_code);
            nationInfo.setRecoder_time(input_time);
            picture_path= picture_path.substring(picture_path.indexOf("nation"));
            nationInfo.setNation_z(nation_z);

            switch(selectFileKind){
                case 1: nationInfo.setMap_picture(picture_path);NationInfoDao.uploadNationFile_Map_picture(nationInfo);break;
                case 2: nationInfo.setNation_emblem(picture_path);NationInfoDao.uploadNationFile_Nation_emblem(nationInfo);break;
                case 3: nationInfo.setNation_flag(picture_path);NationInfoDao.uploadNationFile_Nation_flag(nationInfo);break;
                case 4: nationInfo.setNation_song(picture_path);NationInfoDao.uploadNationFile_Nation_song(nationInfo);break;
            }
        }
    }

    public void uploadNationMessage(List<List<String>> messages,String input_code) {
        String[] contents = new String[9];
        Date currentTime = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String input_time = simpleDateFormat.format(currentTime);
        if (messages != null) {
            for (int i = 0; i < messages.size(); i++) {
                System.out.println("第" + (i + 1) + "行");
                List<String> cellList = messages.get(i);
                for (int j = 0; j < cellList.size(); j++) {

                    contents[j] = cellList.get(j);System.out.print("    第" + (j + 1) + "列值：" + contents[j] + "\t");
                }
                System.out.println();
                TNationInfo tNationInfo = new TNationInfo(contents[0], contents[1], contents[2], contents[3], contents[4], Double.parseDouble(contents[5]), Double.parseDouble(contents[6]), Double.parseDouble(contents[7]), contents[8], input_time, input_code);
                NationInfoDao.uploadNationMessage(tNationInfo);
            }

        }
    }

    @Override
    public void upload_flag_ZIP(List<String> picture_paths, String input_code) {

    }

    @Override
    public List<TNationInfo> findNationInfoByLimit(Map<String,Object> map) {
        return NationInfoDao.findNationInfoByLimit(map);

    }

    @Override
    public TCountry findCountryByName(String name) {
        return NationInfoDao.findCountryByName(name);
    }

    @Override
    public Boolean insertNationInfo(TNationInfo nationInfo) {
        return NationInfoDao.insertNationInfo(nationInfo);
    }

    //    通过中文名字查询国家代码
    public String getCountryCodeByNation_z(String nation_z){
        return NationInfoDao.getCountryCodeByNation_z(nation_z);
    }

    @Override
    public Boolean delete(String pk) {
        return NationInfoDao.delete(pk);
    }

}
